﻿/********************************************************
 * 类名： GoodsCategoryDAL.cs
 * 功能： 商品数据层
 *   ver	变更日		作者			变更内容
 * ────────────────────────────────────────
 *   V01.0	2013-11-27	李波			初版
 *
 * Copyright (c) 2013，李波版权所有
 ********************************************************/
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using PSS.Entity;
using PSS.Entity.SetManage.AuxiliaryInfo;
using PSS.Utility;

namespace PSS.DAL.SetManage.AuxiliaryInfo
{
    public class GoodsCategoryDAL : DbHelperBase
    {
        public int GetTotal()
        {
            var strSql = new StringBuilder();
            strSql.Append(" select  count(*) AS [Total] FROM PSS_GoodsCategory ");
            var count = 0;
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            count = Convert.ToInt32(reader["Total"]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return count;
            }
        }
        public IList<GoodsCategoryEntity> GetGoodsCategoryList(Pagination qcmodel)
        {
            var list = new List<GoodsCategoryEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            var start = qcmodel.PageSize * qcmodel.PageIndex + 1;
            var end = qcmodel.PageSize + start - 1;
            strSql.Append(" SELECT T.* FROM( ");
            strSql.Append(" SELECT row_number() over (order by CreateTime) AS idx,GCategoryId,GCategoryName,CreateTime FROM PSS_GoodsCategory ");
            strSql.Append(" WHERE 1=1 ");
            strSql.Append(") AS T ");
            strSql.Append(" Where T.idx between " + start + " and " + end);
            if (qcmodel.SortField != "" && qcmodel.SortOrder != "")
            {
                strSql.Append(" Order by " + qcmodel.SortField + " " + qcmodel.SortOrder);
            }
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new GoodsCategoryEntity();
                            if (reader["GCategoryId"] != DBNull.Value)
                                model.GCategoryId = new Guid(reader["GCategoryId"].ToString());
                            model.GCategoryName = reader["GCategoryName"] == obj ? null : Convert.ToString(reader["GCategoryName"]);
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }
        public IList<GoodsCategoryEntity> GetGoodsCategoryListByCombobox()
        {
            var list = new List<GoodsCategoryEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            strSql.Append(" SELECT GCategoryId,GCategoryName FROM PSS_GoodsCategory Order by CreateTime ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new GoodsCategoryEntity();
                            if (reader["GCategoryId"] != DBNull.Value)
                                model.GCategoryId = new Guid(reader["GCategoryId"].ToString());
                            model.GCategoryName = reader["GCategoryName"] == obj ? null : Convert.ToString(reader["GCategoryName"]);
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }
        public bool ModifyGoodsCategory(GoodsCategoryEntity model)
        {
            var result = false;
            var strSql = new StringBuilder();
            strSql.Append(" UPDATE [PSS_GoodsCategory] SET [GCategoryName] = @GCategoryName WHERE [GCategoryId] = @GCategoryId ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@GCategoryName", model.GCategoryName);
                    Db.AddInParameter(cmd, "@GCategoryId", model.GCategoryId);
                    result = Db.ExecuteNonQuery(cmd) > 0;
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }
        public bool AddGoodsCategory(GoodsCategoryEntity model)
        {
            var result = false;
            var strSql = new StringBuilder();
            strSql.Append(" INSERT INTO [PSS_GoodsCategory]([GCategoryId],[GCategoryName]) ");
            strSql.Append(" VALUES(@GCategoryId,@GCategoryName) ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@GCategoryId", model.GCategoryId);
                    Db.AddInParameter(cmd, "@GCategoryName", model.GCategoryName);
                    result = Db.ExecuteNonQuery(cmd) > 0;
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }
        public GoodsCategoryEntity GetGoodsCategoryInfo(string id)
        {
            var model = new GoodsCategoryEntity();
            object obj = DBNull.Value;
            var strSql = new StringBuilder();
            strSql.Append(" SELECT [GCategoryId],[GCategoryName] FROM [PSS_GoodsCategory] where [GCategoryId]=@GCategoryId ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {

                try
                {
                    Db.AddInParameter(cmd, "@GCategoryId", id);
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            if (reader["GCategoryId"] != DBNull.Value)
                                model.GCategoryId = new Guid(reader["GCategoryId"].ToString());
                            model.GCategoryName = reader["GCategoryName"] == obj ? null : Convert.ToString(reader["GCategoryName"]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return model;
            }
        }
        public bool RemoveGoodsCategory(string id)
        {
            var result = false;
            var strSql = new StringBuilder();
            strSql.Append(" DELETE FROM [PSS_GoodsCategory] WHERE GCategoryId=@GCategoryId ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@GCategoryId", id);
                    if (Db.ExecuteNonQuery(cmd) > 0)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }
    }
}
